#include "dialogdemo.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QButtonGroup>
#include <QLabel>
#include <QAction>
#include <QDebug>
#include <QMainWindow>
#include <QMenuBar>
InnerDialogDemo::InnerDialogDemo(QWidget *parent) :
    QWidget(parent)
{
    int screenWidth=QApplication::desktop()->width();
    int screenHeight=QApplication::desktop()->height();

    setContentsMargins(30,30,30,30);
    qDebug()<<parent->contentsRect().width()<<"  "<<parent->contentsRect().height();
    if(parent)
    {
        QMainWindow * w=qobject_cast<QMainWindow*>(parent);
        resize(w->centralWidget()->width(),w->centralWidget()->height());
        move(0,w->menuBar()->height());
        qDebug()<<"   "<<w->centralWidget()->width()<<"  "<<w->centralWidget()->height();
    }
    //move(screenWidth/4,screenHeight/4);

    QVBoxLayout *vboxLayout=new QVBoxLayout;
    QGridLayout *gridLayout=new QGridLayout;
    gridLayout->setColumnMinimumWidth(0,80);
    gridLayout->setColumnMinimumWidth(1,80);
    gridLayout->setColumnMinimumWidth(2,80);
    gridLayout->setColumnMinimumWidth(3,80);
    gridLayout->setMargin(10);
    int id=0;
    //0
    QButtonGroup *buttonGroup=new QButtonGroup(this);
    QPushButton *button=new QPushButton("颜色对话框");        
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,0,0);
    //1
    button=new QPushButton("错误信息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,0,1);
    //2
    button=new QPushButton("打开文件对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,0,2);
    //3
    button=new QPushButton("打开多个文件对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,0,3);


    //4
    button=new QPushButton("保存文件对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,1,0);
    //5
    button=new QPushButton("字体对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,1,1);
    //6
    button=new QPushButton("页设置对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,1,2);
    //7
    button=new QPushButton("选择文件夹对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,1,3);
    //8
    button=new QPushButton("进度对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,2,0);
    //9
    button=new QPushButton("打印对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,2,1);
    //10
    button=new QPushButton("字符串输入");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,2,2);
    //11
    button=new QPushButton("下拉列表框输入");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,2,3);
    //12
    button=new QPushButton("Int类型输入");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,3,0);
    //13
    button=new QPushButton("Double类型输入");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,3,1);
    //14
    button=new QPushButton("问题消息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,3,2);
    //15
    button=new QPushButton("信息消息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,3,3);
    //16
    button=new QPushButton("警告消息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,4,0);
    //17
    button=new QPushButton("错误消息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,4,1);
    //18
    button=new QPushButton("关于消息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,4,2);
    //19
    button=new QPushButton("关于Qt消息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,4,3);
    //20
    button=new QPushButton("自定义消息框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,5,0);
    //21
    button=new QPushButton("向导对话框");
    buttonGroup->addButton(button,id++);
    gridLayout->addWidget(button,5,1);

    connect(buttonGroup,SIGNAL(buttonClicked(QAbstractButton*)),this,SLOT(buttonClicked(QAbstractButton*)));
    connect(buttonGroup,SIGNAL(buttonClicked(int)),this,SLOT(buttonClicked(int)));


    mTips=new QTextEdit;
    mTips->setText("Demo");

    gridLayout->setMargin(10);
    gridLayout->setSpacing(10);
    vboxLayout->addLayout(gridLayout);
    vboxLayout->addWidget(mTips);
    setLayout(vboxLayout);

}
#include <QDebug>
void InnerDialogDemo::buttonClicked(QAbstractButton *button)
{
    qDebug()<<"ButtonClicked:"<<button;
    //button->actions()
}
#include <QColorDialog>
#include <QErrorMessage>
#include <QFileDialog>
#include <QFontDialog>
#include <QProgressDialog>
#include <QTest>
#include <QInputDialog>
#include <QMessageBox>
#include <QWizardPage>


void InnerDialogDemo::buttonClicked(int buttonid){
    qDebug()<<"ButtonClicked:"<<buttonid;
    switch(buttonid){
    case 0:{
        //颜色对话框
        QPalette palette=mTips->palette();
        const QColor& color=QColorDialog::getColor(palette.color(QPalette::Base),this);
        if(color.isValid()){
            palette.setColor(QPalette::Text,color);
            mTips->setPalette(palette);
        }
        break;
    }
    case 1:{
        QErrorMessage box(this);
        box.setWindowTitle("title");
        box.showMessage("Message");
        box.exec();
        break;
    }
    case 2:{
        QString filename=QFileDialog::getOpenFileName(this,"打开文件",".","任何文件(*.*);;文本文件(*.txt)");
        if(filename.length()!=0){
            mTips->setText(filename);
        }
        break;
    }
    case 3:{
        QStringList filenames=QFileDialog::getOpenFileNames(this,"打开文件",".","任何文件(*.txt)");
        if(filenames.size()!=0){
            mTips->setText(filenames.join(","));
        }

        break;
    }
    case 4:{
        QFileDialog* filedialog=new QFileDialog(this);
        filedialog->setWindowTitle("Save file");
        filedialog->setFileMode(QFileDialog::AnyFile);
        filedialog->setNameFilter("file(*.*)");
        QString filename;
        if(filedialog->exec()){
            filename=filedialog->getSaveFileName();
        }
        if(filename.size()>0){
            mTips->setText(filename);
        }
        break;
    }
    case 5:{
        bool ok;
        const QFont& font=QFontDialog::getFont(&ok,mTips->font(),this,"字体对话框");
        if(ok){
            mTips->setFont(font);
        }
        break;
    }
    case 6:{
        //QPageSetupDialog
        break;
    }
    case 7:{
        QFileDialog::Options options=QFileDialog::DontResolveSymlinks|QFileDialog::ShowDirsOnly;
        options|=QFileDialog::DontUseNativeDialog;
        QString directory=QFileDialog::getExistingDirectory(this,"open dir",".",options);
        if(directory.length())
            mTips->setText(directory);
        break;
    }
    case 8:{
        QProgressDialog dialog("正在复制","Cancel",0,100,this);
        dialog.setWindowModality(Qt::WindowModal);
        dialog.show();
        for(int i=0;i<100;i++){
            dialog.setValue(i);
            qApp->processEvents();
            if(dialog.wasCanceled())
                break;
            QTest::qWait(1);
        }
        break;
    }
    case 9:{
        //打印对话框QPrintDialog QT += printsupport
        break;
    }
    case 10:{
        bool ok;
        QString name=QInputDialog::getText(this,"getText","Label",QLineEdit::Normal,"name",&ok);
        if(name.isEmpty()==false)
            mTips->setText(name);
        break;
    }
    case 11:{
        bool ok;
        QStringList list;
        list<<"男"<<"女";
        QString sex=QInputDialog::getItem(this,"getItem","label",list,0,false,&ok);
        if(sex.isEmpty()==false)
            mTips->setText(sex);
        break;
    }
    case 12:{
        bool ok;
        int value=QInputDialog::getInt(this,"getInt","label",100,0,255,2,&ok);
        if(ok){
            mTips->setText(QString::number(value));
        }
        break;
    }
    case 13:{
        bool ok;
        double value=QInputDialog::getDouble(this,"getDouble","label",100,0,255,2,&ok);
        if(ok){
            mTips->setText(QString::number(value));
        }
        break;
    }
    case 14:{
        QMessageBox::question(this,"title","text");
        break;
    }
    case 15:{
        QMessageBox::information(this,"提示","提示信息");
        mTips->setText("Information");
        break;
    }
    case 16:{
        QMessageBox::warning(this,"title","提示信息",QMessageBox::Save|QMessageBox::Cancel|QMessageBox::Discard,QMessageBox::Save);
        mTips->setText("warning");
        break;
    }
    case 17:{
        QMessageBox::critical(this,"title","提示信息");
        mTips->setText("critical");
        break;
    }

    case 18:{
        QMessageBox::about(this,"title","text");
        break;
    }
    case 19:{
        QMessageBox::aboutQt(this,"Title");
        break;
    }
    case 20:{
        QMessageBox customBox;
        customBox.setWindowTitle("Title");
        QPushButton* button1=customBox.addButton("加锁",QMessageBox::ActionRole);
        QPushButton* button2=customBox.addButton("解锁",QMessageBox::ActionRole);
        customBox.setText("Message");
        customBox.exec();
        if(customBox.clickedButton()==button1){
            mTips->setText("加锁");
        }
        else if(customBox.clickedButton()==button2){
            mTips->setText("解锁");
        }
        break;
    }
    case 21:{
        QWizard wizard;

        QWizardPage* page=new QWizardPage;
        page->setTitle("First Page");
        QLabel* label=new QLabel("First Page Text");
        label->setWordWrap(true);
        QVBoxLayout* layoutBox=new QVBoxLayout;
        layoutBox->addWidget(label);
        page->setLayout(layoutBox);
        wizard.addPage(page);


        page=new QWizardPage;
        page->setTitle("Second Page");
        label=new QLabel("Second Page Text");
        label->setWordWrap(true);
        layoutBox=new QVBoxLayout;
        layoutBox->addWidget(label);
        page->setLayout(layoutBox);
        wizard.addPage(page);
        break;
    }
    }
}
